package com.example.travelcomponentdemo;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.example.component.GMapV2Direction;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class GetDirectionOnMap extends FragmentActivity implements OnMarkerDragListener {
	/** google map */
	private static final int MAP_ZOOM_SIZE = 14;
	private Document doc;
	GoogleMap mMap;
	GMapV2Direction md;
	LatLngBounds.Builder bc;
	ArrayList<Marker> markerList = new ArrayList<Marker>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_get_direction);
		
		final LatLng fromPosition = new LatLng(10.788353578808298, 106.68552180210352);
		final LatLng toPosition = new LatLng(10.907934353448242, 106.78148478269577);
		
		bc = new LatLngBounds.Builder();
		bc.include(fromPosition);
		bc.include(toPosition);

		mMap = ((SupportMapFragment)getSupportFragmentManager()
		        .findFragmentById(R.id.fragment_map)).getMap();
		mMap.setOnMarkerDragListener(this);
		
		md = new GMapV2Direction();

		GetDirectionDocument getDirection = new GetDirectionDocument(fromPosition, toPosition);
		getDirection.execute("");

	}
	
	class GetDirectionDocument extends AsyncTask<String, Document, Document> {

		private LatLng start, end;
	    public GetDirectionDocument(LatLng start, LatLng end) {
	    	this.start = start;
	    	this.end = end;
	    }

	    @Override
		protected Document doInBackground(String... params) {
			String url = "http://maps.googleapis.com/maps/api/directions/xml?" 
	                + "origin=" + start.latitude + "," + start.longitude  
	                + "&destination=" + end.latitude + "," + end.longitude 
	                + "&sensor=false&units=metric&mode=walking";
			Log.e("GetDirectionDocument", url);
	        try {
	            HttpClient httpClient = new DefaultHttpClient();
	            HttpContext localContext = new BasicHttpContext();
	            HttpPost httpPost = new HttpPost(url);
	            HttpResponse response = httpClient.execute(httpPost, localContext);
	            InputStream in = response.getEntity().getContent();
	            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	            doc = builder.parse(in);
	            
	            return doc;
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return null;
		}
	    
	    @Override
	    protected void onPostExecute(Document result) {
	    	ArrayList<LatLng> directionPoint = md.getDirection(doc);
			PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);

			for(int i = 0 ; i < directionPoint.size() ; i++) {          
				rectLine.add(directionPoint.get(i));
			}

			mMap.addPolyline(rectLine);
			mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), 10));
	     }
	    
	}

	@Override
	public void onMarkerDrag(Marker marker) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMarkerDragEnd(Marker marker) {
		
	}

	@Override
	public void onMarkerDragStart(Marker marker) {
		// TODO Auto-generated method stub
		
	}

}
